package com.mashibing.class07;

/**
 * @Auther: huangxiaode
 * @Date: 2024-02-01 - 02 - 01 - 23:33
 * @Description: com.mashibing.class07
 * @version: 1.0
 */
// letcode 112 : https://leetcode.com/problems/path-sum
public class Code03_PathSum {
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }
    public static boolean isSum = false;
    public static boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        isSum = false;
        process(root,0,sum);
        return isSum;
    }

    public static void process(TreeNode treeNode, int preSum, int sum){
        // 当前节点是叶子节点的时候
        if (treeNode.left==null && treeNode.right==null){
            if (treeNode.val+preSum==sum){
                isSum = true;
            }
            return;
        }
        // 当前节点不是叶子节点的时候
        preSum += treeNode.val;
        if (treeNode.left!=null){
            process(treeNode.left,preSum,sum);
        }
        if (treeNode.right!=null){
            process(treeNode.right,preSum,sum);
        }
    }
}
